import axios from 'axios';

// 获取token
const getToken = () => localStorage.getItem('token');

// 获取帖子列表
export const getPartners = async (params) => {
  try {
    const response = await axios.get('/api/partners', { params });
    return response.data;
  } catch (error) {
    console.error('获取帖子列表失败:', error);
    throw new Error('获取帖子列表失败，请稍后重试');
  }
};

// 创建帖子
export const createPartner = async (postData) => {
  try {
    const response = await axios.post('/api/partners', postData, {
      headers: {
        Authorization: `Bearer ${getToken()}`,
      },
    });
    return response.data;
  } catch (error) {
    console.error('创建帖子失败:', error);
    throw new Error('创建帖子失败，请稍后重试');
  }
};

// 删除帖子
export const deletePartner = async (postId) => {
  try {
    const response = await axios.delete(`/api/partners/${postId}`, {
      headers: {
        Authorization: `Bearer ${getToken()}`,
      },
    });
    return response.data;
  } catch (error) {
    console.error('删除帖子失败:', error);
    throw new Error('删除帖子失败，请稍后重试');
  }
};
